package com.lcz.googleplay.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;

import com.lcz.googleplay.R;
import com.lcz.googleplay.holder.Detail_InfoHolder;
import com.lcz.googleplay.config.DetailProtocol;
import com.lcz.googleplay.domain.DetailInfo;
import com.lcz.googleplay.holder.Detail_SafeHolder;
import com.lcz.googleplay.holder.Detail_ScreenHolder;
import com.lcz.googleplay.utils.GsonUtils;
import com.lcz.googleplay.utils.UIUtils;
import com.lcz.googleplay.widget.LoadingFrameLayout;
import com.lcz.googleplay.widget.ToolBarActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 应用详情页面跟应用程序MainActivity一样，有着加载进度，根据不同状态显示不同页面
 * 在MainActivity中，由Fragment处理，这里则是当前Activity处理
 */
public class DetailActivity extends ToolBarActivity {

    @BindView(R.id.bottom_layout)
    FrameLayout bottomLayout;
    @BindView(R.id.detail_info)
    FrameLayout detail_info;
    @BindView(R.id.detail_safe)
    FrameLayout detail_safe;
    @BindView(R.id.detail_screen)
    HorizontalScrollView detail_screen;
    @BindView(R.id.detail_des)
    FrameLayout detail_des;
    private String packageName;
    private DetailInfo detailInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        packageName = intent.getStringExtra("packageName");
        super.onCreate(savedInstanceState);
        LoadingFrameLayout layout = new LoadingFrameLayout(this) {
            @Override
            public View createSuccessView() {
                return DetailActivity.this.createSuccessView();
            }

            @Override
            public LoadResult load() {
                return DetailActivity.this.load();
            }
        };
        layout.show();  /*根据服务器返回数据切换状态*/
        setContentView(layout);
        setTitle("应用详情");
    }

    /*请求服务器*/
    private LoadingFrameLayout.LoadResult load() {
        DetailProtocol protocol = new DetailProtocol(packageName);
        String json = protocol.load(0);
        System.out.println("DetailActivity.load======------" + json);
        detailInfo = GsonUtils.getInstanceByJson(json, DetailInfo.class);
        if (detailInfo == null) {
            return LoadingFrameLayout.LoadResult.error;
        } else {
            return LoadingFrameLayout.LoadResult.success;

        }
    }

    private View createSuccessView() {
        View view = View.inflate(UIUtils.getActivity(), R.layout.activity_detail, null);
        ButterKnife.bind(this, view);

        //应用信息
        Detail_InfoHolder infoHolder = new Detail_InfoHolder();
        infoHolder.setData(detailInfo);
        detail_info.addView(infoHolder.getView());

        //应用图片预览
        Detail_ScreenHolder screenHolder = new Detail_ScreenHolder();
        screenHolder.setData(detailInfo);
        detail_screen.addView(screenHolder.getView());

        //安全信息提示
        Detail_SafeHolder safeHolder = new Detail_SafeHolder();
        safeHolder.refreshView(detailInfo);
        detail_safe.addView(safeHolder.getView());

        return view;
    }
}
